
class MapManager {
    maps: GameMap[] = [];
    // private maps: { [index: string]: GameMap } = {};
    private container: renderer.DisplayObjectContainer;
    currentMapId = 1;

    viewContaner = new renderer.DisplayObjectContainer();
    private map: GameMap;

    parseFromConfig(obj: any) {
        console.log(obj)

        for (let item of obj.map) {
            const map = new GameMap(item)
            this.maps.push(map)
        }
        // this.map = mapManager.getMap();
        this.maps[this.currentMapId - 1] = new GameMap(obj.map[this.currentMapId - 1]);
        // this.map = new GameMap(obj);
    }
    initContainer(container: renderer.DisplayObjectContainer) {
        this.container = container;
    }
    flagLetter = 0;
    changeMap(a: number, b: number, mapId: number) {

        this.currentMapId = mapId;
        const map = this.getMap();


        this.container.removeAllChildren();
        this.container.addChild(map);

        const mapStartNode = { x: a, y: b };
        map.grid.setStartNode(mapStartNode.x, mapStartNode.y);


        map.addToRoleContainer(user.view)

        //起始地点

        user.view.x = mapStartNode.x * TILE_SIZE;
        user.view.y = mapStartNode.y * TILE_SIZE;

        map.addEventListener("click", (eventData: any) => {
            console.log(222)
            const x = eventData.localX;
            const y = eventData.localY;
            const row = Math.floor(x / TILE_SIZE);
            const col = Math.floor(y / TILE_SIZE);


            const info = map.getTileNodeInfo(row, col);

            pool.clear();
            const walk = new WalkCommand(row, col);
            pool.add(walk);


            const npc = map.getNpcInfo(row, col);
            if (npc) {
                const talk = new TalkCommand(npc);
                pool.add(talk)
                // const gotoNextMap = new GotoNextMapCommand(8, 3, "map1");
                // pool.add(gotoNextMap)
            }

            const item = map.getToolInfo(row, col);

            if (item) {
                if (item.name == "door12") {
                    const gotoNextMap = new GotoNextMapCommand(4, 1, 1);
                    console.log("change map 2")
                    const aside = new GameAsideCommand(2);
                    pool.add(gotoNextMap)
                    pool.add(aside)


                }
                else if (item.name == "door23") {
                    const gotoNextMap = new GotoNextMapCommand(10, 8, 2);
                    console.log("change map 3")
                    const aside = new GameAsideCommand(4);
                    pool.add(gotoNextMap)
                    pool.add(aside)
                }
                else if (item.name == "door34") {
                    const gotoNextMap = new GotoNextMapCommand(1, 4, 3);
                    console.log("change map 4")
                    pool.add(gotoNextMap)
                }
                else if (item.id == 2) {
                    const checkletter = new CheckLetterCommand();

                    pool.add(checkletter);

                    //this.flagLetter++;


                    const aside = new GameAsideCommand(3);
                    pool.add(aside)


                }
                // else if(this.flagLetter==1){
                //     const aside = new GameAsideCommand(3);
                //     pool.add(aside)
                // }
                else if (item.id == 7) {
                    const checkstone = new CheckStoneCommand();

                    pool.add(checkstone);



                }
                else if (item.id == 8) {
                    const pick = new PickCommand(item);
                    const aside = new GameAsideCommand(5);
                    pool.add(aside)
                    pool.add(pick)
                }
                else if (item.name == "door45") {
                    const gotoNextMap = new GotoNextMapCommand(6, 8, 4);
                    console.log("change map 5")
                    pool.add(gotoNextMap)
                }
                else if (item.name == "door56") {
                    const gotoNextMap = new GotoNextMapCommand(9, 8, 5);
                    console.log("change map 6")
                    pool.add(gotoNextMap)
                }
                else if (item.name == "door57") {
                    const gotoNextMap = new GotoNextMapCommand(6, 8, 6);
                    console.log("change map 7")
                    pool.add(gotoNextMap)
                }
                else if (item.name == "door68") {
                    const gotoNextMap = new GotoNextMapCommand(2, 1, 7);
                    console.log("change map 8")
                    pool.add(gotoNextMap)
                }
                else if (item.name == "door78") {
                    const gotoNextMap = new GotoNextMapCommand(9, 7, 7);
                    console.log("change map 8")
                    pool.add(gotoNextMap)
                }
                else if (item.id == 14) {
                    const aside = new GameAsideCommand(6);
                    pool.add(aside);
                }
                else if (item.id == 15) {
                    const aside = new GameAsideCommand(7);

                    pool.add(aside);
                }
                else if (item.name == "door35") {
                    const gotoNextMap = new GotoNextMapCommand(6, 8, 4);
                    console.log("change map 5")
                    pool.add(gotoNextMap)
                }
                else if (item.id == 17) {
                    const choose = new ChosenCommand();

                    pool.add(choose)
                }
                else if (item.id == 24) {
                    const aside = new GameAsideCommand(18);
                    pool.add(aside);
                }
                else {
                    const pick = new PickCommand(item);
                    pool.add(pick)
                }



            }


            pool.execute();

        })
        if (mapId == 5) {
            user.addEventListener("die", (eventData: any) => {
                // if (mapId == 5) {
                const die = new DiedCommand();
                pool.add(die);
                // }
                pool.execute();
            })
            user.addEventListener("spy", (eventData: any) => {
                // if (mapId == 5) {
                const aside = new GameAsideCommand(10);
                pool.add(aside);
                // }
                pool.execute();
            })
        }

    }

    getMap() {
        return this.maps[this.currentMapId]
    }
    choose = 0;
    chosen(m: number) {
        this.choose = m;
    }
}



const RIVER = 0;
const GRESS = 1;
const TILE_SIZE = 64;




class GameMap extends renderer.DisplayObjectContainer {

    // id = 0;
    // name = "";

    grid: astar.Grid
    private config: any;
    private tileConfig: { [index: string]: Tile } = {};
    private toolConfig: { [index: string]: Tool } = {};
    private npcConfig: { [index: string]: Npc } = {};

    private tileContainer = new renderer.DisplayObjectContainer();
    toolContainer = new renderer.DisplayObjectContainer();
    private roleContainer = new renderer.DisplayObjectContainer();


    //private packContainer = new renderer.DisplayObjectContainer();





    constructor(config: any) {
        super();
        this.config = config;

        // this.id = config.id;
        // this.name = config.name;


        this.grid = new astar.Grid(14, 9);
        this.addChild(this.tileContainer);
        this.addChild(this.toolContainer);
        this.addChild(this.roleContainer);
        // this.addChild(this.packContainer);
        // this.addChild(user.putToolContainer);





        for (let item of this.config.setting) {

            const texture = new Image();

            if (item.tile) {
                const tile1 = tileManager.getTileById(item.tile);
                texture.src = tile1.pictureurl;
                const walkable = tile1.walkable;
                const tileview = new renderer.Bitmap(TILE_SIZE * item.x, TILE_SIZE * item.y, texture);
                this.grid.setWalkable(item.x, item.y, walkable);
                this.tileContainer.addChild(tileview);
            }



            if (item.tool) {

                const tool = toolManager.getToolById(item.tool);
                this.toolContainer.addChild(tool.view);
                tool.x = item.x;
                tool.y = item.y;
                tool.view.x = tool.x * TILE_SIZE;
                tool.view.y = tool.y * TILE_SIZE;
                const key1 = tool.x + "_" + tool.y;
                this.toolConfig[key1] = tool;

            }
            if (item.npc) {
                const npc = npcManager.getNpcById(item.npc);
                this.roleContainer.addChild(npc.view);
                npc.x = item.x;
                npc.y = item.y;
                npc.view.x = npc.x * TILE_SIZE;
                npc.view.y = npc.y * TILE_SIZE;
                const key = npc.x + "_" + npc.y;
                this.npcConfig[key] = npc;
            }



        }


        // this.packContainer.addChild(user.packegedContainer);


    }

    addToRoleContainer(child: renderer.DisplayObject) {
        if (child.parent) {
            child.parent.removeChild(child);
        }
        this.roleContainer.addChild(child);
    }

    getToolInfo(x: number, y: number) {
        return this.toolConfig[x + "_" + y];
    }

    getNpcInfo(x: number, y: number) {
        const key = x + "_" + y;
        return this.npcConfig[key]
    }

    removeItem(item: Tool) {

        const key = item.x + "_" + item.y;
        this.toolContainer.removeChild(item.view);
        console.log(item);

        delete this.toolConfig[key];

    }

    getTileNodeInfo(x: number, y: number) {
        for (let item of this.config) {
            if (item.x == x && item.y == y) {
                return item;
            }
        }
        return null;
    }


}



const mapManager = new MapManager();